<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>   
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>用户信息-编辑</title>
	<link type="text/css" rel="stylesheet" href="../<s:property value="#session.themeStyle.mainCss" default="Skin/PM/LightBlue/main.css"/>" />
</head>
<body>
	<div class="navBar">
			当前位置：系统管理&nbsp;&gt;&gt;&nbsp;用户管理 &nbsp;&gt;&gt;&nbsp;<s:if test="user.pkId==null">新建</s:if><s:else>编辑</s:else>用户
	</div>
	<s:form action="userInfo-save" theme="simple">
	<div class="tabBlock">
		<div class="editBlock">
				<table>
					<tbody>
						<tr>
							<th colspan="4"><s:if test="user.pkId==null">新建</s:if><s:else>编辑</s:else>用户</th>
						</tr>  	
						  <tr>
							<th style="color:#000000;width:80px;">
									所属部门：
							</th>
							<td colspan="3">
								<s:if test="user.userDepts.size>0">
								<s:iterator value="user.userDepts" var="userDept" status="st">
									<s:select name="user.userDepts" value="pkId" list="departSelectList" listKey="pkId" listValue="name"></s:select>
									<s:if test="#st.index==0">
										<a class="but28" onclick="delUserDept(this);return false;" style="display:none;">删除</a>
									</s:if><s:else>
										<a class="but28" onclick="delUserDept(this);return false;" style="">删除</a>
									</s:else>
								</s:iterator>
								</s:if><s:else>
									<s:select name="user.userDepts" value="pkId" list="departSelectList" listKey="pkId" listValue="name"></s:select>
									<a class="but28" onclick="delUserDept(this);return false;" style="display:none;">删除</a>
								</s:else>
								<a class="but28" onclick="addUserDept(this);return false;" style="">添加</a>
							</td>
						  </tr>
						  <tr>
						    <th style="color:#000000;">帐号</th>
						    <td ><input class="inputText" id="userName" name="userName" maxlength="40" type="text"  value="${userName}" />
						    </td>
						    <th style="color:#000000;">姓名</th>
						    <td ><input class="inputText" id="name" name="name" maxlength="40" type="text"  value="${name}" /></td>
						  </tr>
						  <tr>
						  	<th style="color:#000000;">密码</th>
						    <td ><input class="inputText" id="password" name="password" maxlength="20" type="text"  value="${password}" /></td>
						  	<th style="color:#000000;">移动电话</th>
						    <td ><input class="inputText" id="telphone" name="telphone" type="text" maxlength="20" value="${telphone}" /></td>
						  </tr>
						  <tr>
						  	<th style="color:#000000;">电子邮件</th>
						    <td ><input class="inputText" id="email" name="email" type="text" maxlength="200"  value="${email}" /></td>
						  	<th style="color:#000000;">通讯地址</th>
						    <td ><input class="inputText" id="address" name="address" type="text" maxlength="200" value="${address}" /></td>
						  </tr>
						  <tr>
						  	<th style="color:#000000;">角色授权</th>
						    <td colspan="3" >
								<s:checkboxlist name="user.roleIds" list="user.refObject['roleList']" listKey="pkId" listValue="name" value="user.roleIds" disabled="true" cssStyle="width:120px"></s:checkboxlist>
								<s:checkboxlist name="user.roleIds" list="user.refObject['availableRoles']" listKey="pkId" listValue="name" value="user.roleIds" cssStyle="width:120px"></s:checkboxlist>
							</td>
						  </tr>
						  <tr>
						    <th colspan="4" style="color:red;text-align:left;padding-left:10px;">${message}</th>
					   	  </tr>
					</tbody>
				</table>
			</div>
			<div class="toolbar" align="center" style="text-align: center;">
				<s:hidden id="pkId" name="pkId"></s:hidden>
				<s:hidden id="deptId" name="deptInfo.pkId"></s:hidden>
				<a class="but28" onclick="submitUser();">保存</a>
			</div>
		</div>
	</s:form>
	<script src="<%=basePath %>js/jquery.js" type="text/javascript"></script>
	<script src="<%=basePath %>js/store.js" type="text/javascript"></script>
	<script type="text/javascript">
	
		function submitUser() {
			var pkId = $.trim($("#pkId").val());
			var userName = $.trim($("#userName").val());
			if(userName==""){
				alert("用户账号不能为空！");
				return false;
			}
			//判断用户账号是否被注册
			$.post("userInfo-checkUserCode.action", {pkId:pkId,userName:userName},
				function(data){
			    	if(data=="1"){
			    		//验证可用
			    		saveUser();
			    	}else{
			    		//验证不可用，账号已经被注册！
			    		alert("账号已存在！");
			    		return false;
			    	}
			});
		}
		
		function saveUser(){
			var deptId = $("#deptId").val();
			var deptIds = getUserDeptIds();
			var pkId = $.trim($("#pkId").val());
			var userName = $.trim($("#userName").val());
			var name = $.trim($("#name").val());
			if(name == ""){
				alert("用户姓名不能为空！");
				return false;
			}
			var password = $.trim($("#password").val());
			var telphone = $.trim($("#telphone").val());
			var email = $.trim($("#email").val());
			var address = $.trim($("#address").val());
			var userRoleIds = getUserRoleIds();
			$.ajax({url: "userInfo-save.action",type: "POST",traditional: true,
				data: {pkId: pkId,userName:userName,name:name,password:password,telphone:telphone,email:email,address:address,userDeptIds:deptIds,userRoleIds:userRoleIds,"deptInfo.pkId":deptId},
				dataType: "json",
				success: function(result){
					if (result.success){
						alert(result.message);
						window.returnValue = "true";
		    			window.close();
					}else{
						alert(result.errorMessage);
					}
				}
			});
			
		}
		
		function delUserDept(_this){
			var prevObj = $(_this).prev().remove();
			$(_this).remove();
		}
		
		function addUserDept(_this){
			var deptSelectObj = $(_this).prev().prev().clone();
			var delBtnObj = $(_this).prev().clone();
			$(delBtnObj).css("display","");
			deptSelectObj.val("");
			$(_this).before(deptSelectObj);
			$(_this).before(delBtnObj);
		}
		
		function getUserDeptIds(){
			var userDeptIds = "";
			$("select[name='user.userDepts']").each(function(){
				if (userDeptIds.length>0)userDeptIds+=",";
				userDeptIds += $(this).val();
			});
			return userDeptIds
		}
		
		function getUserRoleIds(){
			var roleIds = "";
			$("input[name='user.roleIds']:checked").each(function(){
				if (roleIds.length>0)roleIds+=",";
				roleIds += $(this).val();
			});
			return roleIds
		}
	       
		function orgChanged(_this){
			$.ajax({
				url: "userInfo-getDeptSelectList.action",
				type: "POST",
				data: {"user.org.pkId": _this.value},
				dataType: "json",
				success: function(result){
					if (result.success){
						var selectObj = $("#departId");
						selectObj.empty();
						for(var i = 0; i< result.result.length; i++){
							var obj = result.result[i];
							selectObj.append("<option value='"+obj.pkId+"'>"+obj.name+"</option>"); 
						}
					}else{
						alert("调用失败："+result.errorMessage);
					}
				},
				error: function(){
					alert("调用错误！");
				}
			});
		}
		
	</script>
	</body>
</html>